﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessObjects;
using DataAccess;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.DXErrorProvider;

namespace QuanLyHopDong
{
    public partial class GiangVienThinhGiangDetailView : Form
    {
        public event EventHandler SaveChanged;

        private BindingList<GiangVienThinhGiang> bindingListGiangVienThinhGiang;
        private BindingList<BoPhan> bindingListBoPhan;
        private BindingList<TinhTrang> bindingListTinhTrang;

        private BoPhan boPhan;
        private TinhTrang tinhTrang;
          private GiangVienThinhGiang _giangVienThinhGiang;

        public GiangVienThinhGiangDetailView()
        {
            InitializeComponent();
            
        }
        public GiangVienThinhGiangDetailView(GiangVienThinhGiang giangVienThinhGiang)
        {
            InitializeComponent();
            
            InitValidateNgaySinh();

            _giangVienThinhGiang = giangVienThinhGiang;

            bindingListGiangVienThinhGiang = new BindingList<GiangVienThinhGiang>();
            bindingListGiangVienThinhGiang.Add(_giangVienThinhGiang);


            dsGiangVienThinhGiang.DataSource = bindingListGiangVienThinhGiang;
            BindingData();
            layoutControl1.Refresh();
        }
        private void BindingData()
        {
            bindingListBoPhan = new BindingList<BoPhan>(DataAccessObject.BoPhan.GetListBoPhan());
            bindingListTinhTrang = new BindingList<TinhTrang>(DataAccessObject.TinhTrang.GetListTinhTrang());

            bindingSourceBoPhan.DataSource = bindingListBoPhan;
            bindingSourceTinhTrang.DataSource = bindingListTinhTrang;

            if (_giangVienThinhGiang.BoPhan != null)
                glkBoPhan.EditValue = _giangVienThinhGiang.BoPhan.Oid;
            if (_giangVienThinhGiang.TinhTrang != null)
                glkTinhTrang.EditValue = _giangVienThinhGiang.TinhTrang.Oid;
            if (_giangVienThinhGiang.GioiTinh == EGioiTinh.Nam)
                chkGioiTinh.Checked = true;
            else
                chkGioiTinh.Checked = false;
          
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLuuVaDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (_giangVienThinhGiang.Oid == -1)
            {
                DataAccessObject.GiangVienThinhGiang.Insert(_giangVienThinhGiang);
                OnSaveChange();
            }
            else
                DataAccessObject.GiangVienThinhGiang.Update(_giangVienThinhGiang);
            Close();
        }

        void OnSaveChange()
        {
            if (SaveChanged != null)
                SaveChanged(this, EventArgs.Empty);
        }

        private void glkBoPhan_EditValueChanged(object sender, EventArgs e)
        {
            //BoPhan bp = new BoPhan();

            boPhan = gvBoPhan.GetFocusedRow() as BoPhan;
            if (boPhan != null)
                _giangVienThinhGiang.BoPhan = boPhan;
        }

        private void glkTinhTrang_EditValueChanged(object sender, EventArgs e)
        {
            tinhTrang = gvTinhTrang.GetFocusedRow() as TinhTrang;
            if (tinhTrang != null)
                _giangVienThinhGiang.TinhTrang = tinhTrang;
        }



        private void NhanVienDetailView_Load(object sender, EventArgs e)
        {
            //Bộ phận
            string[] listFieldName = new string[] { "MaQuanLy", "TenBoPhan", "TrucThuoc.TenBoPhan" };
            string[] listColumnName = new string[] { "Mã Quản Lý", "Tên Bộ Phận", "Trực Thuộc" };
            AppGridView.InitGridView(gvBoPhan, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, false);
            AppGridView.ReadOnlyGridView(gvBoPhan);
            AppGridView.ShowField(gvBoPhan, listFieldName, listColumnName);
            //gvBoPhan.SetFocusedValue(bindingListBoPhan.FirstOrDefault().Oid);

            //Tình trạng
            listFieldName = new string[] { "MaQuanLy", "TenTinhTrang" };
            listColumnName = new string[] { "Mã Quản Lý", "Tên Tình Trạng" };
            AppGridView.InitGridView(gvTinhTrang, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, false);
            AppGridView.ReadOnlyGridView(gvTinhTrang);
            AppGridView.ShowField(gvTinhTrang, listFieldName, listColumnName);

        }

        private void chkGioiTinh_CheckedChanged(object sender, EventArgs e)
        {
            _giangVienThinhGiang.GioiTinh = (EGioiTinh) chkGioiTinh.EditValue;

        }

        private void InitValidateNgaySinh()
        {
            //DXValidationProvider vpMyValidation = new DXValidationProvider();

            //ConditionValidationRule rangeValidationRule = new ConditionValidationRule();
            //rangeValidationRule.CaseSensitive = true;
            //rangeValidationRule.ConditionOperator = ConditionOperator.Between;


            //rangeValidationRule.Value1 = DateTime.Today.AddYears(-61);

            //rangeValidationRule.Value2 = DateTime.Today.AddYears(-19);

            //rangeValidationRule.ErrorText = "Please enter a value between '01/15/2012' and '01/31/2012'";
            //vpMyValidation.SetValidationRule(dateEdit1, rangeValidationRule);
            //vpMyValidation.ValidationMode = ValidationMode.Auto;
        }
    }
}
